<?php

define('JL', 1);

// RESIZE IMAGE PROPS
define("MAX_WIDTH", 450);
define("MAX_HEIGHT", 420);

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
//    $targetPath = $_SERVER['DOCUMENT_ROOT'] . str_replace('csme.fr', 'csme', $_REQUEST['folder']) . '/';

    $targetPath = $_SERVER['DOCUMENT_ROOT'] .  $_REQUEST['folder'] . '/';
    $targetFile = str_replace('//', '/', $targetPath) . $_FILES['Filedata']['name'];

    if (move_uploaded_file($tempFile, $targetFile)) {

        // RESIZE IMAGE PROPS
        $size = getimagesize($targetFile);

        if ($size[0] > MAX_WIDTH) {
            require_once $_SERVER['DOCUMENT_ROOT'] . '/Libraries/Tools/JLImage.class.php';
//            require_once $_SERVER['DOCUMENT_ROOT'] . '/csme/administration/Libraries/Tools/JLImage.class.php';

            $image = new JLImage();
            $image->load($targetFile);
            $image->resizeToWidth(MAX_WIDTH);
            $image->save();
        }

        echo '1';
    }
}